//
//  NetWorkingTask.m
//  SAFramework
//
//  Created by 王建男 on 15/3/7.
//  Copyright (c) 2015年 WJN_work@163.com. All rights reserved.
//

#import "SANetWorkingTask.h"

@implementation SANetWorkingTask

+ (void)request:(NSString *)URL parmater:(NSDictionary *)dictionary block:(void (^)(NSDictionary *))block {
	
	if ([SAReachabilityManager sharedReachabilityManager].currentReachabilityStatus == NotReachable) {
		
		return;
	}
	
    NSString *resultStr = URL;
    CFStringRef originalString = (__bridge CFStringRef)URL;
    CFStringRef leaveUnescaped = CFSTR(" ");
    CFStringRef forceEscaped = CFSTR("!*'();@+$,#[]");
    CFStringRef escapedStr;
    
    escapedStr = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                         originalString,
                                                         leaveUnescaped,
                                                         forceEscaped,
                                                         kCFStringEncodingUTF8);
    if(escapedStr)
    {
        NSMutableString *mutableStr = [NSMutableString stringWithString:(__bridge NSString *)escapedStr];
        CFRelease(escapedStr);
        
        // replace spaces with plusses
        [mutableStr replaceOccurrencesOfString:@" "
                                    withString:@""
                                       options:0
                                         range:NSMakeRange(0, [mutableStr length])];
        resultStr = mutableStr;
    }
    
    URL = resultStr;

    
	AFHTTPRequestOperationManager *mannager = [AFHTTPRequestOperationManager manager];
	[mannager.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", nil]];
	[mannager GET:URL parameters:dictionary success:^(AFHTTPRequestOperation *operation, id responseObject) {
	
		NSDictionary *data = [NSJSONSerialization JSONObjectWithData:operation.responseData options:NSJSONReadingMutableContainers error:NULL];
		
		block(data);
	} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
		[KVNProgress dismiss];
	}];
}

@end
